<?php

class User extends MY_Controller {
    private $per_page  = '10';
    private $prev_link  = 'Prev';
    private $next_link  = 'Next';
    
    public function __construct(){
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('index_model');
    }
    
    public function index(){
        if($this->is_logged_in()){
            $config = array();
            $config['per_page']    = $this->per_page;         //每页显示的数据数
            $current_page          = intval($this->input->get_post('per_page',true));  //获取当前分页页码数
            $condition = $this->input->post('condition') == NULL?
                    $this->input->get('condition'):$this->input->post('condition');
            if(0 == $current_page){
               $current_page = 1;
            }
            $offset   = ($current_page - 1 ) * $config['per_page'];          //设置偏移量 限定 数据查询 起始位置（从 $offset 条开始）       
            $user  = $this->user_model->get_user($offset,$config['per_page'],$order='lastlogintime desc',$condition);
            $config['base_url']           = base_url('user/index?').'condition='.$condition."&";
            $config['prev_link']          = $this->prev_link;
            $config['next_link']          = $this->next_link;
            $config['total_rows']         = $user['total'];         //获取查询数据的总记录数
            $config['use_page_numbers']   = TRUE;            //默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码
            $config['page_query_string']  = TRUE;            //把 $config['enable_query_strings'] 设置为 TRUE，链接将自动地被用查询字符串（url中的参数）重写。        
            $this->pagination->initialize($config);

            $data = array(
                'user'  => $user['res'],
                'total'   => $user['total'],
                'current_page' => $current_page,
                'per_page'  => $config['per_page'],
                'page'   => $this->pagination->create_links(),
                'condition'  =>  $condition,
            );
            $this->load->view('templates/header');
            $this->load->view('templates/left');
            $this->load->view('user/user_top');
            $this->load->view('user/index',$data);
            $this->load->view('user/user_bottom');
            $this->load->view('templates/footer');
        }else{
            redirect('user/login');
        }
    }

    public function loginlog(){
        if($this->is_logged_in()){
            $config = array();
            $config['per_page']    = $this->per_page;         //每页显示的数据数
            $current_page          = intval($this->input->get_post('per_page',true));  //获取当前分页页码数
            $condition = $this->input->post('condition') == NULL?
                    $this->input->get('condition'):$this->input->post('condition');
            if(0 == $current_page){
               $current_page = 1;
            }
            $offset   = ($current_page - 1 ) * $config['per_page'];          //设置偏移量 限定 数据查询 起始位置（从 $offset 条开始）       
            $user  = $this->user_model->get_login_log($offset,$config['per_page'],$order='logintime desc',$condition);
            
            $config['base_url']           = base_url('user/index?').'condition='.$condition."&";
            $config['prev_link']          = $this->prev_link;
            $config['next_link']          = $this->next_link;
            $config['total_rows']         = $user['total'];         //获取查询数据的总记录数
            $config['use_page_numbers']   = TRUE;            //默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码
            $config['page_query_string']  = TRUE;            //把 $config['enable_query_strings'] 设置为 TRUE，链接将自动地被用查询字符串（url中的参数）重写。        
            $this->pagination->initialize($config);

            $data = array(
                'user'  => $user['res'],
                'total'   => $user['total'],
                'current_page' => $current_page,
                'per_page'  => $config['per_page'],
                'page'   => $this->pagination->create_links(),
                'condition'  =>  $condition,
            );
            $this->load->view('templates/header');
            $this->load->view('templates/left');
            $this->load->view('user/user_top');
            $this->load->view('user/loginlog',$data);
            $this->load->view('user/user_bottom');
            $this->load->view('templates/footer');
        }else{
            redirect('user/login');
        }
    }
    
    public function login(){
        if($this->is_logged_in()){
            redirect('index/index');
        }else{
            $this->form_validation->set_rules('username', 'Username', 'trim|required');
            $this->form_validation->set_rules('password', 'Password', 'trim|required');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('user/login');
            } else {
                $username = $this->input->post('username');
                $password = $this->input->post('password');
                $result = $this->user_model->login($username, $password);
                if($result){
                    redirect('index/index');
                } else {
                    $data['error'] = "Error username or password,please try again.";
                    $this->load->view('user/login',$data);
                }
            }
        }
    }
    
    public function new_user(){
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header');
            $this->load->view('templates/left');
            $this->load->view('user/user_top');
            $this->load->view('user/new_user');
            $this->load->view('user/user_bottom');
            $this->load->view('templates/footer');
        }else{
            $this->user_model->newuser();
            redirect("user/index");
        }
    }
    
    public function delete_user(){
        $this->user_model->delete_user();
        redirect("user/index");
    }

    public function delete_log(){
        $this->user_model->delete_log();
        redirect("user/loginlog");
    }
    
    public function employee(){
        if($this->is_logged_in()){
            $config = array();
            $config['per_page']    = $this->per_page;         //每页显示的数据数
            $current_page          = intval($this->input->get_post('per_page',true));  //获取当前分页页码数
            $condition = $this->input->post('condition') == NULL?
                    $this->input->get('condition'):$this->input->post('condition');
            if(0 == $current_page){
               $current_page = 1;
            }
            $offset   = ($current_page - 1 ) * $config['per_page'];          //设置偏移量 限定 数据查询 起始位置（从 $offset 条开始）       
            $employee  = $this->user_model->get_employee($offset,$config['per_page'],$order='id desc',$condition);
            $config['base_url']           = base_url('user/employee?').'condition='.$condition."&";
            $config['prev_link']          = $this->prev_link;
            $config['next_link']          = $this->next_link;
            $config['total_rows']         = $employee['total'];         //获取查询数据的总记录数
            $config['use_page_numbers']   = TRUE;            //默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码
            $config['page_query_string']  = TRUE;            //把 $config['enable_query_strings'] 设置为 TRUE，链接将自动地被用查询字符串（url中的参数）重写。        
            $this->pagination->initialize($config);

            $data = array(
                'employee'  => $employee['res'],
                'total'   => $employee['total'],
                'current_page' => $current_page,
                'per_page'  => $config['per_page'],
                'page'   => $this->pagination->create_links(),
                'condition'  =>  $condition,
            );
            $this->load->view('templates/header');
            $this->load->view('templates/left');
            $this->load->view('user/user_top');
            $this->load->view('user/employee',$data);
            $this->load->view('user/user_bottom');
            $this->load->view('templates/footer');
        }else{
            redirect('user/login');
        }
    }
    
    public function new_employee(){
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('userid', 'User', 'trim|required');
        if ($this->form_validation->run() == FALSE) {
            $data['leisureuser'] = $this->user_model->getleisureuser();
            $this->load->view('templates/header');
            $this->load->view('templates/left');
            $this->load->view('user/user_top');
            $this->load->view('user/new_employee',$data);
            $this->load->view('user/user_bottom');
            $this->load->view('templates/footer');
        }else{
            $this->user_model->newemployee();
            redirect("user/employee");
        }
    }
    
    public function delete_employee(){
        $this->user_model->delete_employee();
        redirect("user/employee");
    }
    
    public function logout(){
        $this->session->unset_userdata('user');
        redirect('user/login');
    }
    
    public function employee_detail($id){
        if(!$_POST){
        }else{
            $userid = $this->input->post("userid");
            //根据员工id跟新员工信息
            $data = array(
                'sn'=>$this->input->post("sn"),
                'name'=>$this->input->post("name"),
                'department'=>$this->input->post("department"),
                'position'=>$this->input->post("position"),
                'workpassholder'=>$this->input->post("workpassholder"),
                'joindate'=>$this->input->post("joindate"),
                'birthday'=>$this->input->post("birthday"),
                'userid'=>$userid
            );
            $this->index_model->update("employee",$data,$id);
            $data = array(
                'password'=>$this->input->post("password")
            );
            $this->index_model->update("user",$data,$userid);
        }
        //根据employeeid得到用户登录信息和员工信息
        $employee = $this->index_model->getByID("employee",$id);
        $userid = $employee['userid'];
        $user = $this->user_model->getuserbyid($userid);
        $leisureuser = $this->user_model->getleisureuser();
        $data = array(
            'user'=>$user,
            'employee'=>$employee,
            'leisureuser'=>$leisureuser
        );
        $this->load->view('templates/header');
        $this->load->view('templates/left');
        $this->load->view('user/user_top');
        $this->load->view('user/employee_edit',$data);
        $this->load->view('user/user_bottom');
        $this->load->view('templates/footer');
    }
    
    function set_language(){
        $this->load->library('session');
        $data = $this->input->post('language');
        if($data =='chinese'){
            $this->session->set_userdata('language','chinese');
        }else{
            $this->session->set_userdata('language','english');
        }
        echo json_encode(array('msg'=>$data));
        return;
    }
}

